package cn.zhangjie.org.service.impl;

import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.org.domain.Department;
import cn.zhangjie.org.mapper.DepartmentMapper;
import cn.zhangjie.org.query.DepartmentQuery;
import cn.zhangjie.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void add(Department department) {

        departmentMapper.save(department);
        //如果没有选择上级部门
        if(department.getParent_id()==null){
            Long id = department.getId();
            //只添加自己的id - 自增长id
            department.setDirPath("/"+id);
        }else {
            //获取上级部门id
            Long parent_id = department.getParent_id();
            //获取上级部门的dirPath
            Department parentdepartment = departmentMapper.loadById(parent_id);
            //组装自己的id
            department.setDirPath(parentdepartment.getDirPath()+"/"+department.getId());
        }
        //更新到数据库
        update(department);
    }

    @Transactional
    @Override
    public void update(Department department) {


        if(department.getParent_id()==null){
            Long id = department.getId();
            department.setDirPath("/"+id);
        }else {
            Long parent_id = department.getParent_id();
            Department parentdepartment = departmentMapper.loadById(parent_id);
            department.setDirPath(parentdepartment.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);

    }

    @Override
    public List<Department> deptTree() {
        //查询所有
        List<Department> all = departmentMapper.loadAll();
        //准备一个map
        HashMap<Long,Department>  map = new HashMap<>();
        //把集合放在map中
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //准备一个集合保存处理后的数据
        ArrayList<Department> deptTree = new ArrayList<>();

        for (Department department : all) {
            if(department.getParent_id()==null){
                deptTree.add(department);
            }else {
                Long parent_id = department.getParent_id();
                Department department1 = map.get(parent_id);
                department1.getChildren().add(department);
            }
        }
        return deptTree;
    }
}